import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/lock/api.dart";
import "package:ct_pa_majordomo/pages/device/lock/electronic_key/widgets/expiration_date/index.dart";
import "package:ct_pa_majordomo/pages/device/lock/electronic_key/widgets/ope_record/index.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/router/keys.dart";
import "package:ct_pa_majordomo/utils/date_tools.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";
import "package:ct_pa_majordomo/widgets/tai_dialog/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class Detail extends StatefulWidget {
  final Map lockInfo;
  const Detail({super.key, required this.lockInfo});

  @override
  State<StatefulWidget> createState() => _DetailState();

}

class _DetailState extends State<Detail> {


  Future<void> _delKey(int lockId, int keyId) async {
    print("keyId:  $keyId, lockInfo: ${widget.lockInfo}");
    // /// 删除钥匙
    taiDialog(
      context: context,
      title: "确定删除钥匙？",
      child: const Text("删除钥匙后，钥匙将无法使用。",textAlign: TextAlign.center,),
      onConfirm: (BuildContext taiContext) async {
        try {
          EasyLoading.show();
          await lockApi.deleteKey(lockId, keyId);
          EasyLoading.showToast("删除钥匙成功");
          Stacker.pushNamedAndRemoveUntil(Keys.DEVICE_LOCK_ELECTRONICKEY, ModalRoute.withName(Keys.DEVICE_LOCK_DETAIL), arguments: lockId);
        } catch (error) {
          print("error: $error");
        }
      },
    );
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: const IconThemeData(color: Colors.white),
        title: const Text("钥匙详情", style: TextStyle(color: Colors.white),),
        centerTitle: true,
        backgroundColor: ThemeVars.colorPrimaryDark,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios_new),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      body: ListView(
        children: [
          MenuBlock(
            borderRadius: BorderRadius.circular(0),
            items: [
              MenuBlockItem(
                text: "姓名",
                showArrow: false,
                suffix: Text(widget.lockInfo["keyName"] ?? "", style: TextStyle(color: ThemeVars.colorTextTer),),
              ),
              // MenuBlockItem(
              //   text: "有效期",
              //   suffix: const Text("永久", style: TextStyle(color: ThemeVars.colorTextTer),),
              //   onTap: () {
              //     Stacker.pushNamed(Keys.DEVICE_LOCK_ELECTRONICKEY_EXPIRATION_DATE);
              //   }
              // ),
            ],
          ),
          const SizedBox(height: 12,),
          MenuBlock(
            borderRadius: BorderRadius.circular(0),
            items: [
              MenuBlockItem(
                text: "接收者账号",
                showArrow: false,
                suffix: Text(widget.lockInfo["member"]["phonenumber"] ?? "", style: TextStyle(color: ThemeVars.colorTextTer),),
              ),
              MenuBlockItem(
                text: "发送人",
                showArrow: false,
                suffix: Text(widget.lockInfo["senderUsername"], style: TextStyle(color: ThemeVars.colorTextTer),)
              ),
              MenuBlockItem(
                text: "发送时间",
                showArrow: false,
                suffix: Text(DateTools.formatYMD(widget.lockInfo["date"], format: "{y}-{m}-{d} {h}:{i}:{s}"), style: TextStyle(color: ThemeVars.colorTextTer),)
              ),
            ],
          ),
          const SizedBox(height: 12,),
          MenuBlock(
            borderRadius: BorderRadius.circular(0),
            items: [
              MenuBlockItem(
                text: "操作记录",
                onTap: () {
                  Stacker.pushNamed(Keys.DEVICE_LOCK_ELECTRONICKEY_OPE_RECORD, arguments: widget.lockInfo);
                },
              ),
            ],
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(ThemeVars.paddingPage, 60, ThemeVars.paddingPage, 0),
            child: Button(
              radius: 6,
              type: ButtonType.normal,
              onPressed: () {},
              child: const Text("冻结",),
            ),
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(ThemeVars.paddingPage, 8, ThemeVars.paddingPage, 0),
            child: Button(
              radius: 6,
              type: ButtonType.danger,
              onPressed: () {
                _delKey(int.parse(widget.lockInfo["lock"]["lockId"]), widget.lockInfo["keyId"]);
              },
              child: const Text("删除",),
            ),
          ),
        ],
      ),
    );
  }

}